package com.zhuangfei.hpuhelper.tools;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class ViewTools {

	public static File viewSaveToImage(View view,String fileName) {
		view.setDrawingCacheEnabled(true);
		view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
		view.setDrawingCacheBackgroundColor(Color.WHITE);

		// 把一个View转换成图片
		Bitmap cachebmp = loadBitmapFromView(view);


		File file=FileTools.saveViewImage(cachebmp,"viewimg_"+fileName);

		view.destroyDrawingCache();
		
		return file;
		
	}

	private static Bitmap loadBitmapFromView(View v) {
		int w = v.getWidth();
		int h = v.getHeight();

		Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(bmp);

		c.drawColor(Color.WHITE);
		/** 如果不设置canvas画布为白色，则生成透明 */

		v.layout(0, 0, w, h);
		v.draw(c);

		return bmp;
	}
	
}
